package com.lishe.lishe.lishe.module.auth.http;

import android.content.Context;
import android.util.Log;

import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcase on 2017/9/6.
 */

public class AuthHttpHelper {

    private static final String TAG = AuthHttpHelper.class.getSimpleName();

    //private static final String RELATIVE_URL_doLogin = SIGN_URL+"login";
    //private static final String RELATIVE_URL_doLogout = SIGN_URL+"logout";

    private static final String SIGN_URL = "Home/Sign/";

    private static final String LOGIN_URL = "Home/Login/";

    public static String phoneOfReg;

    public static String phoneOfActive;
    public static String checkCodeOfActive;

    public static String phoneOfForgetPwd;

    public static String phoneOfChangePwd;

    public static String phoneOfChangePayPwd;

//    /**
//     * 自动登录
//     *
//     * @param callBack
//     */
//    public static void doAutoLogin(final Context ctx, final LSWebHelper.WebCallBack callBack) {
//
//        final String userName = SharedPreferencesUtils.getUserNameSaved(ctx);
//        final String password = SharedPreferencesUtils.getPasswordSaved(ctx);
////        if (!(TextUtils.isEmpty(userName)&& TextUtils.isEmpty(password))) {
//        doLogin(ctx, userName, password, callBack);
////        }
//    }


    /**
     * 用户登录
     *
     * @param userName
     * @param password
     * @param callBack
     */
    public static void doLogin(final Context ctx, final String userName, final String password, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "login";

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("userName", userName);
        params.put("password", password);
        SharedPreferencesUtils.removeToken(ctx);
        SharedPreferencesUtils.removeUserId(ctx);

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, new LSWebHelper.WebCallBack() {

            @Override
            public void onSuccess(JSONObject dataJson, String dataString, String message) {

                Log.e(TAG, message);
                Log.e("登陆", message);
                Log.e("1登陆", dataJson.toString());

                if (dataJson != null) {
                    String token = dataJson.optString("token");
                    String userId = dataJson.optString("id");
                    if (token != null && !token.equals("")) {
                        SharedPreferencesUtils.saveToken(token,ctx);
                        SharedPreferencesUtils.saveUserId(userId,ctx);
                        if (callBack != null) {
                            callBack.onSuccess(dataJson, dataString, message);
                            return;
                        }
                    }
                }

                if (callBack != null) {
                    callBack.onFailure("服务器参数有误！");
                    return;
                }


            }

            @Override
            public void onFailure(String message) {
                if (callBack != null) {
                    callBack.onFailure(message);
                }
            }
        });
    }

    /**
     * 用户登出
     *
     * @param ctx
     * @param callBack
     */
    public static void doLogout(final Context ctx, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "logout";

        LSWebHelper.getInstance().postAsynUseToken(relativeUrl, null, new LSWebHelper.WebCallBack() {

            @Override
            public void onSuccess(JSONObject dataJson, String dataString, String message) {
                /*SharedPreferencesUtils.removeUserId(ctx);
                SharedPreferencesUtils.removeToken(ctx);*/
                SharedPreferencesUtils.clearUserAll();

                if (callBack != null) {
                    callBack.onSuccess(dataJson, dataString, message);
                    return;
                }


            }

            @Override
            public void onFailure(String message) {
                if (callBack != null) {
                    callBack.onFailure(message);
                }
            }
        });
    }

    /**
     * 发送注册验证码接口
     *
     * @param ctx
     * @param params
     * @param callBack
     */
    public static void doCheckSignUp(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = LOGIN_URL + "sendRegisterVerifyCode";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }

    /**
     * 用户注册：比对验证码
     *
     * @param ctx
     * @param params
     * @param callBack
     */
    public static void doCheckRegisterMsgVerifyCode(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = LOGIN_URL + "checkRegisterMsgVerifyCode";
        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }

    /**
     * 检查用户是否激活
     *
     * @param ctx
     * @param params   手机号码
     * @param callBack
     */
    public static void doCheckMemberPost(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "checkMemberPost";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }

    /**
     * 检查用户token是否失效（用户是否在登录状态）
     *
     * @param callBack
     */
    public static void doCheckToken(final Context ctx, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "checkToken";

        LSWebHelper.getInstance().postAsynUseToken(relativeUrl, null, new LSWebHelper.WebCallBack() {

            @Override
            public void onSuccess(JSONObject dataJson, String dataString, String message) {

                Log.e(TAG, message);

                if (dataJson != null) {
                    boolean status = dataJson.optBoolean("status", false);
                    if (!status) {
                        SharedPreferencesUtils.removeToken(ctx);
                    } else {

                    }
                    if (callBack != null) {
                        callBack.onSuccess(dataJson, dataString, message);
                        return;
                    }
                }

            }

            @Override
            public void onFailure(String message) {
                SharedPreferencesUtils.removeToken(ctx);
                if (callBack != null) {
                    callBack.onFailure(message);
                }
            }
        });
    }

    /**
     * 用户激活：第一步，获取验证码接口
     *
     * @param ctx
     * @param params
     * @param callBack
     */
    public static void doCheckActivate(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "checkActivate";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }


    /**
     * 用户激活：最后一步
     *
     * @param ctx
     * @param params   手机号码
     * @param callBack
     */
    public static void doActivite(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "activate";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }


    /**
     * 用户注册：最后一步
     *
     * @param ctx
     * @param params
     * @param callBack
     */
    public static void doRegister(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = LOGIN_URL + "memberRegister";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, new LSWebHelper.WebCallBack() {
            @Override
            public void onSuccess(JSONObject dataJson, String dataString, String message) {

                Log.e(TAG, message);

                if (dataJson != null) {
                    String token = dataJson.optString("token");
                    if (token != null && !token.equals("")) {
                        SharedPreferencesUtils.saveToken(token, ctx);
                        if (callBack != null) {
                            callBack.onSuccess(dataJson, dataString, message);
                            return;
                        }
                    }
                }

                if (callBack != null) {
                    callBack.onFailure("服务器参数有误！");
                    return;
                }


            }

            @Override
            public void onFailure(String message) {
                if (callBack != null) {
                    callBack.onFailure(message);
                }
            }

        });
    }


    /**
     * 鉴权:找回密码 获取验证短信
     *
     * @param params
     * @param callBack
     */
    public static void doGetLoginPassCheckCode(HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "sendRetrievedMsgVerifyCode";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }

    /**
     * 鉴权:找回密码 比对验证码
     *
     * @param ctx
     * @param params
     * @param callBack
     */
    public static void doCheckRetrievedMsgVerifyCode(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "checkRetrievedMsgVerifyCode";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }

    /**
     * 鉴权:找回密码 密码设置（最后一步）
     *
     * @param ctx
     * @param params
     * @param callBack
     */
    public static void doSetLoginPassword(final Context ctx, HashMap<String, Object> params, final LSWebHelper.WebCallBack callBack) {
        String relativeUrl = SIGN_URL + "setLoginPassword";

        LSWebHelper.getInstance().postAsyn(relativeUrl, params, callBack);
    }

}
